package net.wesley.android;

import java.net.URLEncoder;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public abstract class City implements OnRequestParamsFinish {

	private TableLayout paramsTable=null;
	protected Context ctx;
	protected String chepai;
	protected OnQueryComplete callback;

	public abstract boolean match(String chepai);
	public abstract String[] paramDesc();
	//public abstract void doQuery(Context weizhang, String chepai, OnQueryComplete callback);
	protected void requestParams(Context c, String cp, String[] params,
			OnRequestParamsFinish callback) {
		this.ctx=c;
		this.chepai=cp;
		final OnRequestParamsFinish cb=callback;
		if (params!=null){
			Builder builder = new AlertDialog.Builder(ctx).setTitle("请输入以下信息：").setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					final ArrayList<String> values = new ArrayList<String>();
					SharedPreferences settings = ctx.getSharedPreferences(weizhang.PREFS_NAME, 0);  
					SharedPreferences.Editor editor = settings.edit();  
					for (int i=0;i<paramsTable.getChildCount();i++){
						TableRow r=(TableRow) paramsTable.getChildAt(i);
						String v=((EditText)r.getChildAt(1)).getText().toString().toUpperCase();
						values.add(v);
						editor.putString(chepai+"p"+i, v);  
					}

					editor.commit(); 
					new Handler().postDelayed(new Runnable(){

						public void run() {
							cb.requestParamsFinish(values.toArray(new String[values.size()]));
						}
						
					}, 1000);
				}
			});
			paramsTable=City.updateParams(ctx, cp,builder, params);
			builder.create().show();
		}
		else{
			cb.requestParamsFinish(null);
		}
	}

	public static TableLayout updateParams(Context ctx, String cp, Builder build, String[] descs) {
		TableLayout paramTable = new TableLayout(ctx);
		paramTable.setColumnStretchable(1, true);
        SharedPreferences settings = ctx.getSharedPreferences(weizhang.PREFS_NAME, 0);  
		for (int i=0;i<descs.length;i++){
			TableRow r=new TableRow(ctx);
			TextView v=new TextView(ctx);
			v.setText(descs[i]+":");
			r.addView(v);
	        String v0 = settings.getString(cp+"p"+i,"");  
			EditText t=new EditText(ctx);
			t.setText(v0);
			r.addView(t,new TableRow.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
			paramTable.addView(r,new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
		}
		build.setView(paramTable);
		return paramTable;
	}

	public void doQuery(Context ctx, String chepai,
			OnQueryComplete callback) {
		this.chepai=chepai;
		this.ctx=ctx;
		this.callback=callback;
		requestParams(ctx, chepai, paramDesc(), this);
	}



}
